package com.quectel.cms.controller.patrol;


import cn.hutool.core.collection.CollectionUtil;
import com.quectel.business.common.device.DeviceExtraAttrCls;
import com.quectel.business.dms.device.DeviceIntegrationBusiness;
import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.device.dto.DeviceDto;
import com.quectel.core.module.patrol.dto.VideoPatrolTaskDto;
import com.quectel.core.module.patrol.service.VideoPatrolTaskService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2020-07-07 15:47:56
 */
@RestController
@RequestMapping("videoPatrolTask")
@Tag(name = "VideoPatrolTaskController", description = "视频巡更任务相关api")
public class VideoPatrolTaskController extends BaseController {

    @DubboReference
    private VideoPatrolTaskService videoPatrolTaskService;
    @Autowired
    private DeviceIntegrationBusiness deviceIntegrationBusiness;

    /**
     * 视频巡更任务统计信息
     */
    @GetMapping("taskExecInfo")
    @RequirePermissions(values = "videoPatrolTask:list")
    @Operation(summary = "视频巡更任务统计信息")
    public Response<Object> taskExecInfo() {
        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        Map<String, Long> result = videoPatrolTaskService.taskExecInfo(params);
        //查询列表数据
        return Response.ok().wrap(result);
    }

    /**
     * 视频巡更任务分页查询列表
     */
    @GetMapping("list")
//    @RequirePermissions(values ="videoPatrolTask:list")
    @Operation(summary = "视频巡更任务分页查询列表")
    public Response<Page<VideoPatrolTaskDto>> list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Request request = Request.configParams(params).like("execUsername");

        super.fillAuthorityData(params);

        int total = videoPatrolTaskService.queryTotal(params);

        List<VideoPatrolTaskDto> list = null;
        if (total > 0) {
            list = videoPatrolTaskService.queryList(params);
        } else {
            list = new ArrayList<>();
        }

        Page<VideoPatrolTaskDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<VideoPatrolTaskDto>>ok().wrap(page);
    }


    /**
     * 根据id查询视频巡更任务记录
     */
    @GetMapping("info/{id}")
//    @RequirePermissions(values ="videoPatrolTask:info")
    @Operation(summary = "根据id查询视频巡更任务记录")
    public Response<VideoPatrolTaskDto> info(@PathVariable("id") Long id) {

        VideoPatrolTaskDto videoPatrolTaskDto = videoPatrolTaskService.selectById(id);
        if (videoPatrolTaskDto != null && CollectionUtil.isNotEmpty(videoPatrolTaskDto.getDeviceDtoList())) {

            deviceIntegrationBusiness.fillDevicesStreamUrl(videoPatrolTaskDto.getDeviceDtoList());

            List<String> streams = new ArrayList<>();
            for (DeviceDto deviceDto : videoPatrolTaskDto.getDeviceDtoList()) {
                DeviceExtraAttrCls.AttrOfIntelligentMonitor attr = DeviceExtraAttrCls.parseExtStr(DeviceExtraAttrCls.AttrOfIntelligentMonitor.class
                        , deviceDto.getExtraAttr());

                streams.add(attr.getStreamUrl());
            }

            videoPatrolTaskDto.setStreamUrls(streams);

        }
        return Response.<VideoPatrolTaskDto>ok().wrap(videoPatrolTaskDto);
    }

    /**
     * 保存视频巡更任务记录
     */
    @PostMapping("save")
    @RequirePermissions(values = "videoPatrolTask:edit")
    @Operation(summary = "保存视频巡更任务记录")
    public Response<Object> save(@RequestBody @Validated VideoPatrolTaskDto videoPatrolTaskDto) {

        //填充基础数据
        fillSaveCommonData(videoPatrolTaskDto);

        videoPatrolTaskService.save(videoPatrolTaskDto);

        return Response.ok();
    }

    /**
     * 更新视频巡更任务记录
     */
    @PostMapping("update")
    @RequirePermissions(values = "videoPatrolTask:edit")
    @Operation(summary = "更新视频巡更任务记录")
    public Response<Object> update(@RequestBody @Validated VideoPatrolTaskDto videoPatrolTaskDto) {

        //填充基础数据
        fillEditCommonData(videoPatrolTaskDto);

        videoPatrolTaskService.updateById(videoPatrolTaskDto);

        return Response.ok();
    }

    /**
     * 批量删除视频巡更任务记录
     */
    @PostMapping("delete")
    @RequirePermissions(values = "videoPatrolTask:delete")
    @Operation(summary = "批量删除视频巡更任务记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        videoPatrolTaskService.deleteBatch(ids);

        return Response.ok();
    }
}
